<?php


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
class Cors
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->getMethod() == 'OPTIONS') {
            return response('')->header('Access-Control-Allow-Origin', '*')
                ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
                ->header('Access-Control-Allow-Headers', 'x-requested-with,DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization,Token');
        }

        $headers = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
            'Access-Control-Allow-Headers' => 'x-requested-with,DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization,Token'

        ];
        $response = $next($request);
        foreach($headers as $key => $value) {
            $response->headers->set($key, $value);
        }
        return $response;
    }
}
